<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/2/6
 * Time: 16:49
 */

namespace App\Http\Model;


use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;

class WarehouseLogModel
{
    const TABLE = 'warehouse_log';

    const PURCHASE = 1;//采购
    const RECEIPT = 2;//调拨接收
    const EXPEND = 3;//消耗
    const CONSUME = 4;//个人领用
    const ALLOT = 5;//调拨
    const BREAKDOWN = 6;//报废
    const GROUP_CONSUME = 7;//班组领用
    const RETURNED_PURCHASE = 8;//班组领用

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        return DB::table(self::TABLE)->insertGetId($data);
    }

    /**
     * @param array $data
     * @return string
     * @throws ApiException
     */
    public function addLog(array $data)
    {
        $insertId = '';
        try {
            $insertId = DB::transaction(function () use ($data, $insertId) {
                $warehouseLog = [
                    'projectId' => $data['projectId'],
                    'time' => $data['time'],
                    'remark' => $data['remark'],
                    'type' => $data['type'],
                    'price' => '',
                    'adminId' => $data['adminId'],
                ];
                if (isset($data['sourceEmployeeId'])){
                    $warehouseLog['sourceEmployeeId'] = $data['sourceEmployeeId'];
                }
                if (isset($data['sourceProjectId'])){
                    $warehouseLog['sourceProjectId'] = $data['sourceProjectId'];
                }
                if (isset($data['sourceGroupId'])){
                    $warehouseLog['sourceGroupId'] = $data['sourceGroupId'];
                }

                $insertId = $this->insert($warehouseLog);
                $warehouseLogInfoModel = new WarehouseLogInfoModel();
                if (isset($data['data'])){
                    foreach ($data['data'] as $d){
                        $warehouseLogInfo = [
                            'logId' => $insertId,
                            'materialId' => $d['materialId'],
                            'specId' => $d['specId'],
                            'amount' => $d['amount'],
                            'price' => $d['price'],
                            'totalPrice' => $d['amount']*$d['price'],
                            'unitId' => $d['unitId'],
                            'surplus' => $d['surplus']
                        ];
                        $warehouseLogInfoModel->insert($warehouseLogInfo);
                    }
                }
                return $insertId;
            });
            return $insertId;
        } catch (\Exception $e) {
            throw new ApiException('['.$e->getLine().']['.$e->getFile().']'.$e->getMessage(), 460505);
        }
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getSum(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use($data){
                if (!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
                if (!empty($data['sourceEmployeeId'])){
                    $query->where(self::TABLE.'.sourceEmployeeId',$data['sourceEmployeeId']);
                }
                if (!empty($data['sourceGroupId'])){
                    $query->where(self::TABLE.'.sourceGroupId',$data['sourceGroupId']);
                }
                if (!empty($data['startTime'])){
                    $query->where('time','>=',$data['startTime']);
                }
                if (!empty($data['endTime'])){
                    $query->where('time','<=',$data['endTime']);
                }
                if (!empty($data['type'])){
                    $query->where(self::TABLE.'.type',$data['type']);
                }
                if (isset($data['status'])){
                    if (!is_array($data['status'])){
                        $query->where('status',$data['status']);
                    }else{
                        $query->whereIn('status',$data['status']);
                    }
                }
            })->sum('price');
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getConsumeEmployee(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.sourceEmployeeId')
            ->where(function($query)use($data){
                if (!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('time',[$data['startTime'],$data['endTime']]);
                }
                if (!empty($data['type'])){
                    $query->where(self::TABLE.'.type',$data['type']);
                }
            })
            ->groupBy('sourceEmployeeId')
            ->select('sourceEmployeeId','e.name')
            ->get()->toArray();
    }

    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use($where){
                if (!empty($where['id'])){
                    $query->where('id',$where['id']);
                }
                if (!empty($where['projectId'])){
                    $query->where('projectId',$where['projectId']);
                }
                if (!empty($where['sourceEmployeeId'])){
                    $query->where('sourceEmployeeId',$where['sourceEmployeeId']);
                }
                if (!empty($where['sourceGroupId'])){
                    $query->where('sourceGroupId',$where['sourceGroupId']);
                }
                if (!empty($where['type'])){
                    $query->where('type',$where['type']);
                }
                if (!empty($where['status'])){
                    $query->where('status',$where['status']);
                }
                if (!empty($where['startTime']) && !empty($where['endTime'])){
                    $query->whereBetween('time',[$where['startTime'],$where['endTime']]);
                }
            })
            ->update($data);
    }

    public function getLogs(array $data){
        return DB::table(self::TABLE)
            ->where(function($query) use ($data){
                if (!empty($data['projectId'])){
                    $query->where('projectId',$data['projectId']);
                }
                if (!empty($data['type'])){
                    $query->where('type',$data['type']);
                }
                if (!empty($data['sourceEmployeeId'])){
                    $query->where('sourceEmployeeId',$data['sourceEmployeeId']);
                }
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->whereBetween('time',[$data['startTime'],$data['endTime']]);
                }
            })
            ->get()->toArray();

    }
}